//
//  showInfoWindow.m
//  ElektroM
//
//  Created by Michael on 01.11.09.
//  Copyright 2009 Artgruppe inc. All rights reserved.
//

#import "showInfoWindow.h"
#import "sightseeingInfo.h"
#import "dataBase.h"
#include "Constants.h"

@implementation showInfoWindow
@synthesize table;
@synthesize ssWindow;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationItem.title=NSLocalizedString(@"Info", @"");
}

- (void)viewWillAppear:(BOOL)animated{
	//[self.navigationController setToolbarHidden:NO];
	[self.navigationController setNavigationBarHidden:NO animated:NO];
	[self.navigationController.navigationBar setHidden:NO];
	//[self.navigationController.toolbar setHidden:NO];
	//if(_diAllStationsWithIndexes==nil)
	//[self loadAllNeededElements:_db];
}

-(void) setStationInfo:(int)nFirst andSecond:(int)nSecond
{
    nFirstStation = nFirst;
    nSecondStation = nSecond;

    [self readDataFromDB];

    [table reloadData];
}

-(void) readDataFromDB
{
    dataBase* db=[dataBase alloc];
    db.databaseName=kDBUnderground;
    [db initialization];

    NSMutableArray* aiNumbers=[NSMutableArray arrayWithCapacity: 2];
    if(nFirstStation!=-1)
	[aiNumbers addObject: [NSNumber numberWithInt:nFirstStation]];
    if(nSecondStation!=-1)
	[aiNumbers addObject: [NSNumber numberWithInt: nSecondStation]];
    _aiSightCaptions=[db getSightseeingForNumbers: aiNumbers];
    _aiSightTypes=[db getSightTypes];
    _aiSightDetail=[db getSightDetails];
    _aiSightStationIds=[db getSightStationIds];
}

/*
- (void)viewWillDisappear:(BOOL)animated{
	//[self.navigationController setToolbarHidden:YES];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    //[self releaseUnneededElements];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{    
	return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_aiSightCaptions count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	return @"";	
}

#pragma mark -
#pragma mark Rows Naming
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{		
	static NSString *kCellIdentifier = @"cellID";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
	if (!cell)
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellIdentifier] autorelease];
        
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.textLabel.backgroundColor = [UIColor clearColor];
		cell.textLabel.opaque = NO;
		cell.textLabel.textColor = [UIColor blackColor];
		cell.textLabel.highlightedTextColor = [UIColor whiteColor];
		cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
		
		cell.detailTextLabel.backgroundColor = [UIColor clearColor];
		cell.detailTextLabel.opaque = NO;
		cell.detailTextLabel.textColor = [UIColor grayColor];
		cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
		cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
    }   
	
	cell.textLabel.text = [_aiSightCaptions objectAtIndex: indexPath.row];

	cell.detailTextLabel.text = 
	    [_aiSightStationIds objectAtIndex: indexPath.row];	
	
	cell.imageView.image = [UIImage imageNamed:@"chirch.png"];
	
	return cell;
}

#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
	
	if(self.ssWindow!=nil)
	{
		//[self.ssWindow release];
		self.ssWindow=nil;
	}
	
	sightseeingInfo* targetViewController = [[sightseeingInfo alloc] init];
	[targetViewController init];	
	targetViewController.strCaption = [_aiSightCaptions objectAtIndex: indexPath.row];
	targetViewController.strContents = [_aiSightDetail objectAtIndex: indexPath.row];
	[targetViewController loadView];	
	self.ssWindow=targetViewController;	
	//[targetViewController release];
	
	[self.navigationController pushViewController:self.ssWindow animated:YES];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
